home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
6_14.lha
/
6_14
/
tstc.c
< prev
next >
Wrap
Text File
|
1993-08-08
|
1KB
|
99 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
include <stream.h>
include <error.h>
include <string.h>
include "6_14c.c"
tring::string()
p = new srep;
p->s = 0;
p->n = 1;
tring::string(char* s)
p = new srep;
p->s = new char[ strlen(s) + 1 ];
strcpy(p->s, s);
p->n = 1;
tring::string(string &x)
x.p->n++;
p = x.p;
tring::~string()
if (--p->n == 0) {
delete p->s;
delete p;
}
tring &string::operator=(char *s)
if (p->n > 1) {
p->n--;
p = new srep;
} else if (p->n == 1)
delete p->s;
p->s = new char[strlen(s) + 1];
strcpy(p->s, s);
p->n = 1;
return *this;
tring &string::operator=(string &x)
x.p->n++;
if (--p->n == 0) {
delete p->s;
delete p;
}
p = x.p;
return *this;
stream &operator<<(ostream &out, string &x)
return out << x.p->s << " [" << x.p->n << "]\n";
stream &operator>>(istream &s, string &x)
char buf[256];
s >> buf;
x = buf;
cout << "echo: " << x << "\n";
return s;
ain()
string s = "abc";
cout << "s = " << s << "\n";
int i = s == "def";
cout << "i = " << i << "\n";
string x[100];
int n;
cout << "here we go\n";
for (n = 0; cin >> x[n]; n++) {
string y;
if (n == 100) error("too many strings");
cout << (y = x[n]);
if (y=="done") break;
}
cout << "here we go back again\n";
for (i = n-1; 0<=i; i--)
cout << x[i];
return 0;